package tran;

import org.apache.rocketmq.client.producer.*;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;

import javax.xml.bind.Marshaller;
import java.nio.charset.StandardCharsets;

public class TranProducer {
	public static void main(String[] args) throws Exception {
		// 创建监听器对象
		MyTranListener myTranListener = new MyTranListener();
		// 实例化消息生产者Producer
		TransactionMQProducer producer = new TransactionMQProducer("tran2_group_name");
		// 设置监听器
		producer.setTransactionListener(myTranListener);
		// 设置NameServer的地址
    	producer.setNamesrvAddr("localhost:9876");
    	// 启动Producer实例
        producer.start();
		// 创建消息，并指定Topic，Tag和消息体
		Message msg = new Message("tran2_order_topic" /* Topic */,
				"tran2_employee_tag" /* Tag */,
				"事务消息".getBytes(StandardCharsets.UTF_8)
		);
		// 发送消息到一个Broker
		TransactionSendResult transactionSendResult = producer.sendMessageInTransaction(msg, "事务消息的跟屁虫");
		// 通过sendResult返回消息是否成功送达
		System.out.println("发送成功,状态："+transactionSendResult.getSendStatus());
    	// 如果不再发送消息，关闭Producer实例。
    	producer.shutdown();
    }
}